﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Text;

namespace LSW.Wcf.App
{
    public class MSSQLFactory:IDisposable
    {
        private SqlConnection conn = new SqlConnection();

        public IDbTransaction BeginTransaction()
        {
            return this.conn.BeginTransaction();
        }

        public IDbTransaction BeginTransaction(IsolationLevel iso)
        {
            return this.conn.BeginTransaction(iso);
        }

        public IDbTransaction BeginTransaction(string transactionName)
        {
            return this.conn.BeginTransaction(transactionName);
        }

        public IDbTransaction BeginTransaction(IsolationLevel iso, string transactionName)
        {
            return this.conn.BeginTransaction(iso, transactionName);
        }

        public SqlConnection Connection { get { return conn; } }
        public void close()
        {
            try
            {
                this.conn.Close();
                this.conn = null;
            }
            catch
            {
            }
        }

        public IDataReader getBLOB(string query, CommandType commandType)
        {
            SqlCommand command = new SqlCommand
            {
                CommandTimeout = 0,
                CommandType = commandType,
                CommandText = query,
                Connection = this.conn
            };
            return command.ExecuteReader(CommandBehavior.SequentialAccess);
        }

        public bool open(string connectStr)
        {
            try
            {
                if (this.conn == null)
                {
                    this.conn = new SqlConnection();
                }
                if (this.conn.State != ConnectionState.Open)
                {
                    this.conn.ConnectionString = connectStr;
                    this.conn.Open();
                }
            }
            catch
            {
                return false;
            }
            return true;
        }

        public void runDynamicSqlNoQuery(string query, DbParameter[] parameters, CommandType commandType)
        {
            try
            {
                SqlCommand command = new SqlCommand
                {
                    CommandType = commandType,
                    CommandText = query,
                    Connection = this.conn
                };
                if (parameters != null)
                {
                    for (int i = 0; i < parameters.Length; i++)
                    {
                        command.Parameters.Add((SqlParameter)parameters[i].Parameter);
                    }
                }
                command.ExecuteNonQuery();

                command.Parameters.Clear();
            }
            catch (SqlException exception)
            {
                throw exception;
            }
        }

        public int runDynamicSqlNoQuery(string query, DbParameter[] parameters, CommandType commandType, IDbTransaction trans)
        {
            int num = 0;
            try
            {
                SqlCommand command = new SqlCommand
                {
                    CommandTimeout = 0,
                    CommandType = commandType,
                    Transaction = (SqlTransaction)trans,
                    CommandText = query,
                    Connection = this.conn
                };

                if (parameters != null)
                {
                    for (int i = 0; i < parameters.Length; i++)
                    {
                        command.Parameters.Add((SqlParameter)parameters[i].Parameter);
                    }
                }
                num = command.ExecuteNonQuery();

                

                command.Parameters.Clear();
            }
            catch (SqlException exception)
            {
                throw exception;
            }
            return num;
        }

        public int runDynamicSqlNoQuery(string query, string[] paramNames, object[] paramValues, CommandType commandType)
        {
            return this.runDynamicSqlNoQuery(query, paramNames, paramValues, null, commandType);
        }

        public int runDynamicSqlNoQuery(string query, string[] paramNames, object[] paramValues, ParameterDirection[] paramDirections, CommandType commandType)
        {
            int num;
            try
            {
                SqlCommand command = new SqlCommand
                {
                    CommandTimeout = 0,
                    CommandType = commandType,
                    CommandText = query,
                    Connection = this.conn
                };
                if (paramNames == null)
                {
                    paramNames = MSSQLFactory.generateParamNames(query);
                }
                if (paramNames != null)
                {
                    for (int i = 0; i < paramValues.Length; i++)
                    {
                        SqlParameter parameter;
                        if (paramValues[i] is StringBuilder)
                        {
                            parameter = new SqlParameter
                            {
                                ParameterName = paramNames[i]
                            };
                            if (paramDirections != null)
                            {
                                parameter.Direction = paramDirections[i];
                            }
                            if (parameter.Direction != ParameterDirection.Output)
                            {
                                parameter.Value = ((StringBuilder)paramValues[i]).ToString();
                            }
                        }
                        else if (paramValues[i] is byte[])
                        {
                            byte[] buffer = (byte[])paramValues[i];
                            parameter = new SqlParameter(paramNames[i], SqlDbType.Image, buffer.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, buffer);
                        }
                        else
                        {
                            parameter = new SqlParameter
                            {
                                ParameterName = paramNames[i]
                            };
                            if (paramDirections != null)
                            {
                                parameter.Direction = paramDirections[i];
                            }
                            if (paramValues[i] != null)
                            {
                                parameter.Value = paramValues[i];
                            }
                        }
                        command.Parameters.Add(parameter);
                    }
                }
                num = command.ExecuteNonQuery();
                
            }
            catch (SqlException exception)
            {
                throw exception;
            }
            return num;
        }

        public int runDynamicSqlNoQuery(string query, string[] paramNames, object[] paramValues, CommandType commandType, IDbTransaction trans)
        {
            int num;
            try
            {
                SqlCommand command = new SqlCommand
                {
                    CommandTimeout = 0,
                    CommandType = commandType,
                    Transaction = (SqlTransaction)trans,
                    CommandText = query,
                    Connection = this.conn
                };
                if (paramNames == null)
                {
                    paramNames = MSSQLFactory.generateParamNames(query);
                }
                if (paramNames != null)
                {
                    for (int i = 0; i < paramValues.Length; i++)
                    {
                        SqlParameter parameter;
                        if (paramValues[i] is StringBuilder)
                        {
                            parameter = new SqlParameter
                            {
                                ParameterName = paramNames[i]
                            };
                            if (parameter.Direction != ParameterDirection.Output)
                            {
                                parameter.Value = ((StringBuilder)paramValues[i]).ToString();
                            }
                        }
                        else if (paramValues[i] is byte[])
                        {
                            byte[] buffer = (byte[])paramValues[i];
                            parameter = new SqlParameter(paramNames[i], SqlDbType.Image, buffer.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, buffer);
                        }
                        else
                        {
                            parameter = new SqlParameter
                            {
                                ParameterName = paramNames[i]
                            };
                            if (paramValues[i] != null)
                            {
                                parameter.Value = paramValues[i];
                            }
                        }
                        command.Parameters.Add(parameter);
                    }
                }
                num = command.ExecuteNonQuery();
                
            }
            catch (SqlException exception)
            {
                throw exception;
            }
            return num;
        }

        public DataSet runDynamicSqlQuery(string query, DbParameter[] parameters, CommandType commandType)
        {
            DataSet set;
            try
            {
                SqlDataAdapter adapter = new SqlDataAdapter();
                SqlCommand command = new SqlCommand
                {
                    CommandType = commandType,
                    CommandText = query,
                    Connection = this.conn
                };
                adapter.SelectCommand = command;
                if (parameters != null)
                {
                    for (int i = 0; i < parameters.Length; i++)
                    {
                        command.Parameters.Add((SqlParameter)parameters[i].Parameter);
                    }
                }
                DataSet dataSet = new DataSet();
                adapter.Fill(dataSet);
                command.Parameters.Clear();
                set = dataSet;
            }
            catch (SqlException exception)
            {
                throw exception;
            }
            return set;
        }

        public IDataReader runDynamicSqlQuery(string query, DbParameter[] parameters, CommandType commandType, CommandBehavior behavior)
        {
            IDataReader reader;
            try
            {
                SqlCommand command = new SqlCommand
                {
                    CommandType = commandType,
                    CommandText = query,
                    Connection = this.conn
                };
                if (parameters != null)
                {
                    for (int i = 0; i < parameters.Length; i++)
                    {
                        command.Parameters.Add((SqlParameter)parameters[i].Parameter);
                    }
                }
                IDataReader reader2 = command.ExecuteReader(behavior);
                command.Parameters.Clear();
                reader = reader2;
            }
            catch (SqlException exception)
            {
                throw exception;
            }
            return reader;
        }

        public DataSet runDynamicSqlQuery(string query, string[] paramNames, object[] paramValues, CommandType commandType)
        {
            DataSet set;
            try
            {
                SqlDataAdapter adapter = new SqlDataAdapter();
                SqlCommand command = new SqlCommand
                {
                    CommandTimeout = 0,
                    CommandType = commandType,
                    CommandText = query,
                    Connection = this.conn
                };
                adapter.SelectCommand = command;
                if (paramNames == null)
                {
                    paramNames = MSSQLFactory.generateParamNames(query);
                }
                if (paramNames != null)
                {
                    for (int i = 0; i < paramValues.Length; i++)
                    {
                        SqlParameter parameter = new SqlParameter(paramNames[i], paramValues[i]);
                        command.Parameters.Add(parameter);
                    }
                }
                DataSet dataSet = new DataSet("MyDataSet");
                adapter.Fill(dataSet, "MyTableName");
                set = dataSet;
            }
            catch (SqlException exception)
            {
                throw exception;
            }
            return set;
        }

        public DataSet runDynamicSqlQuery(string query, DbParameter[] parameters, CommandType commandType, int startIndex, int maxRecords)
        {
            DataSet set;
            try
            {
                SqlDataAdapter adapter = new SqlDataAdapter();
                SqlCommand command = new SqlCommand
                {
                    CommandType = commandType,
                    CommandText = query,
                    Connection = this.conn
                };
                adapter.SelectCommand = command;
                if (parameters != null)
                {
                    for (int i = 0; i < parameters.Length; i++)
                    {
                        command.Parameters.Add((SqlParameter)parameters[i].Parameter);
                    }
                }
                DataSet dataSet = new DataSet();
                adapter.Fill(dataSet, startIndex, maxRecords, "MyTableName");
                command.Parameters.Clear();
                set = dataSet;
            }
            catch (SqlException exception)
            {
                throw exception;
            }
            return set;
        }

        public IDataReader runDynamicSqlQuery(string query, string[] paramNames, object[] paramValues, CommandType commandType, CommandBehavior behavior)
        {
            IDataReader reader;
            try
            {
                SqlCommand command = new SqlCommand
                {
                    CommandTimeout = 0,
                    CommandType = commandType,
                    CommandText = query,
                    Connection = this.conn
                };
                if (paramNames == null)
                {
                    paramNames = MSSQLFactory.generateParamNames(query);
                }
                if (paramNames != null)
                {
                    for (int i = 0; i < paramValues.Length; i++)
                    {
                        SqlParameter parameter = new SqlParameter(paramNames[i], paramValues[i]);
                        command.Parameters.Add(parameter);
                    }
                }
                reader = command.ExecuteReader(behavior);
            }
            catch (SqlException exception)
            {
                throw exception;
            }
            return reader;
        }

        public DataSet runDynamicSqlQuery(string query, string[] paramNames, object[] paramValues, CommandType commandType, IDbTransaction trans)
        {
            DataSet set;
            try
            {
                SqlDataAdapter adapter = new SqlDataAdapter();
                SqlCommand command = new SqlCommand
                {
                    CommandTimeout = 0,
                    CommandType = commandType,
                    Transaction = (SqlTransaction)trans,
                    CommandText = query,
                    Connection = this.conn
                };
                adapter.SelectCommand = command;
                if (paramNames == null)
                {
                    paramNames = MSSQLFactory.generateParamNames(query);
                }
                if (paramNames != null)
                {
                    for (int i = 0; i < paramValues.Length; i++)
                    {
                        SqlParameter parameter = new SqlParameter(paramNames[i], paramValues[i]);
                        command.Parameters.Add(parameter);
                    }
                }
                DataSet dataSet = new DataSet("MyDataSet");
                adapter.Fill(dataSet, "MyTableName");
                set = dataSet;
            }
            catch (SqlException exception)
            {
                throw exception;
            }
            return set;
        }

        public DataSet runDynamicSqlQuery(string query, string[] paramNames, object[] paramValues, CommandType commandType, DataSet ds, string tableName)
        {
            DataSet set;
            try
            {
                SqlDataAdapter adapter = new SqlDataAdapter();
                SqlCommand command = new SqlCommand
                {
                    CommandTimeout = 0,
                    CommandType = commandType,
                    CommandText = query,
                    Connection = this.conn
                };
                adapter.SelectCommand = command;
                if (paramNames == null)
                {
                    paramNames = MSSQLFactory.generateParamNames(query);
                }
                if (paramNames != null)
                {
                    for (int i = 0; i < paramValues.Length; i++)
                    {
                        SqlParameter parameter = new SqlParameter(paramNames[i], paramValues[i]);
                        command.Parameters.Add(parameter);
                    }
                }
                adapter.Fill(ds, tableName);
                set = ds;
            }
            catch (SqlException exception)
            {
                throw exception;
            }
            return set;
        }

        public DataSet runDynamicSqlQuery(string query, string[] paramNames, object[] paramValues, CommandType commandType, int startIndex, int maxRecords)
        {
            DataSet set;
            try
            {
                SqlDataAdapter adapter = new SqlDataAdapter();
                SqlCommand command = new SqlCommand
                {
                    CommandTimeout = 0,
                    CommandType = commandType,
                    CommandText = query,
                    Connection = this.conn
                };
                adapter.SelectCommand = command;
                if (paramNames == null)
                {
                    paramNames = MSSQLFactory.generateParamNames(query);
                }
                if (paramNames != null)
                {
                    for (int i = 0; i < paramValues.Length; i++)
                    {
                        SqlParameter parameter = new SqlParameter(paramNames[i], paramValues[i]);
                        command.Parameters.Add(parameter);
                    }
                }
                DataSet dataSet = new DataSet("MyDataSet");
                adapter.Fill(dataSet, startIndex, maxRecords, "MyTableName");
                set = dataSet;
            }
            catch (SqlException exception)
            {
                throw exception;
            }
            return set;
        }

        public object runDynamicSqlScalar(string query, DbParameter[] parameters, CommandType commandType)
        {
            object obj2;
            try
            {
                SqlCommand command = new SqlCommand
                {
                    CommandType = commandType,
                    CommandText = query,
                    Connection = this.conn
                };
                if (parameters != null)
                {
                    for (int i = 0; i < parameters.Length; i++)
                    {
                        command.Parameters.Add((SqlParameter)parameters[i].Parameter);
                    }
                }
                object obj3 = command.ExecuteScalar();
                command.Parameters.Clear();
                obj2 = obj3;
            }
            catch (SqlException exception)
            {
                throw exception;
            }
            return obj2;
        }

        public object runDynamicSqlScalar(string query, string[] paramNames, object[] paramValues, CommandType commandType)
        {
            object obj2;
            try
            {
                SqlCommand command = new SqlCommand
                {
                    CommandTimeout = 0,
                    CommandType = commandType,
                    CommandText = query,
                    Connection = this.conn
                };
                if (paramNames == null)
                {
                    paramNames = MSSQLFactory.generateParamNames(query);
                }
                if (paramNames != null)
                {
                    for (int i = 0; i < paramNames.Length; i++)
                    {
                        SqlParameter parameter;
                        if (paramValues[i] is StringBuilder)
                        {
                            parameter = new SqlParameter
                            {
                                ParameterName = paramNames[i],
                                Value = ((StringBuilder)paramValues[i]).ToString()
                            };
                        }
                        else if (paramValues[i] is byte[])
                        {
                            byte[] buffer = (byte[])paramValues[i];
                            parameter = new SqlParameter(paramNames[i], SqlDbType.Image, buffer.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, buffer);
                        }
                        else
                        {
                            parameter = new SqlParameter
                            {
                                ParameterName = paramNames[i]
                            };
                            if (paramValues[i] != null)
                            {
                                parameter.Value = paramValues[i];
                            }
                        }
                        command.Parameters.Add(parameter);
                    }
                }
                obj2 = command.ExecuteScalar();
            }
            catch (SqlException exception)
            {
                throw exception;
            }
            return obj2;
        }

        public object runDynamicSqlScalar(string query, string[] paramNames, object[] paramValues, CommandType commandType, IDbTransaction trans)
        {
            object obj2;
            try
            {
                SqlCommand command = new SqlCommand
                {
                    CommandTimeout = 0,
                    CommandType = commandType,
                    Transaction = (SqlTransaction)trans,
                    CommandText = query,
                    Connection = this.conn
                };
                if (paramNames == null)
                {
                    paramNames = MSSQLFactory.generateParamNames(query);
                }
                if (paramNames != null)
                {
                    for (int i = 0; i < paramNames.Length; i++)
                    {
                        SqlParameter parameter;
                        if (paramValues[i] is StringBuilder)
                        {
                            parameter = new SqlParameter
                            {
                                ParameterName = paramNames[i],
                                Value = ((StringBuilder)paramValues[i]).ToString()
                            };
                        }
                        else if (paramValues[i] is byte[])
                        {
                            byte[] buffer = (byte[])paramValues[i];
                            parameter = new SqlParameter(paramNames[i], SqlDbType.Image, buffer.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, buffer);
                        }
                        else
                        {
                            parameter = new SqlParameter
                            {
                                ParameterName = paramNames[i]
                            };
                            if (paramValues[i] != null)
                            {
                                parameter.Value = paramValues[i];
                            }
                        }
                        command.Parameters.Add(parameter);
                    }
                }
                obj2 = command.ExecuteScalar();
            }
            catch (SqlException exception)
            {
                throw exception;
            }
            return obj2;
        }

        public int runSQLNoQuery(string query)
        {
            int num;
            try
            {
                SqlCommand command = new SqlCommand(query, this.conn) { CommandTimeout = 0 };
                num = command.ExecuteNonQuery();
                
            }
            catch (SqlException exception)
            {
                throw exception;
            }
            return num;
        }

        public int runSQLNoQuery(string query, CommandType commandType)
        {
            int num;
            try
            {
                SqlCommand command = new SqlCommand(query, this.conn) { CommandType = commandType, CommandTimeout = 0 };
                num = command.ExecuteNonQuery();
                
            }
            catch (SqlException exception)
            {
                throw exception;
            }
            return num;
        }

        public DataSet runSQLQuery(string query)
        {
            return this.runSQLQuery(query, "MyDataSet", "MyTable");
        }

        public DataSet runSQLQuery(string query, CommandType commandType)
        {
            return this.runSQLQuery(query, "MyDataSet", "MyTable", commandType);
        }

        public DataSet runSQLQuery(string query, CommandType commandType, IDbTransaction trans)
        {
            DataSet dataSet = new DataSet();
            try
            {
                SqlDataAdapter adapter = new SqlDataAdapter();
                SqlCommand command = new SqlCommand(query, this.conn)
                {
                    CommandTimeout = 0,
                    CommandType = commandType,
                    Transaction = (SqlTransaction)trans
                };
                adapter.SelectCommand = command;
                adapter.Fill(dataSet);
            }
            catch (SqlException exception)
            {
                throw exception;
            }
            return dataSet;
        }

        public DataSet runSQLQuery(string query, DataSet ds, string tableName)
        {
            return this.runSQLQuery(query, ds, tableName, 0, 0);
        }

        public DataSet runSQLQuery(string query, int startIndex, int maxRecords)
        {
            return this.runSQLQuery(query, "MyDataSet", "MyTable", startIndex, maxRecords);
        }

        public DataSet runSQLQuery(string query, string datasetName, string tableName)
        {
            return this.runSQLQuery(query, new DataSet(datasetName), tableName, 0, 0);
        }

        public DataSet runSQLQuery(string query, DataSet ds, string tableName, CommandType commandType)
        {
            return this.runSQLQuery(query, ds, tableName, 0, 0, commandType);
        }

        public DataSet runSQLQuery(string query, int startIndex, int maxRecords, CommandType commandType)
        {
            return this.runSQLQuery(query, "MyDataSet", "MyTable", startIndex, maxRecords, commandType);
        }

        public DataSet runSQLQuery(string query, string datasetName, string tableName, CommandType commandType)
        {
            return this.runSQLQuery(query, new DataSet(datasetName), tableName, 0, 0, commandType);
        }

        public DataSet runSQLQuery(string query, DataSet ds, string tableName, CommandType commandType, IDbTransaction trans)
        {
            try
            {
                SqlDataAdapter adapter = new SqlDataAdapter();
                SqlCommand command = new SqlCommand(query, this.conn)
                {
                    CommandTimeout = 0,
                    CommandType = commandType,
                    Transaction = (SqlTransaction)trans
                };
                adapter.SelectCommand = command;
                adapter.Fill(ds, tableName);
            }
            catch (SqlException exception)
            {
                throw exception;
            }
            return ds;
        }

        public DataSet runSQLQuery(string query, DataSet ds, string tableName, int startIndex, int maxRecords)
        {
            try
            {
                SqlDataAdapter adapter = new SqlDataAdapter();
                SqlCommand command = new SqlCommand(query, this.conn)
                {
                    CommandTimeout = 0
                };
                adapter.SelectCommand = command;
                if (maxRecords != 0)
                {
                    adapter.Fill(ds, startIndex, maxRecords, tableName);
                    return ds;
                }
                adapter.Fill(ds, tableName);
            }
            catch (SqlException exception)
            {
                throw exception;
            }
            return ds;
        }

        public DataSet runSQLQuery(string query, string datasetName, string tableName, int startIndex, int maxRecords)
        {
            return this.runSQLQuery(query, new DataSet(datasetName), tableName, startIndex, maxRecords);
        }

        public DataSet runSQLQuery(string query, DataSet ds, string tableName, int startIndex, int maxRecords, CommandType commandType)
        {
            try
            {
                SqlDataAdapter adapter = new SqlDataAdapter();
                SqlCommand command = new SqlCommand(query, this.conn)
                {
                    CommandTimeout = 0,
                    CommandType = commandType
                };
                adapter.SelectCommand = command;
                if (maxRecords != 0)
                {
                    adapter.Fill(ds, startIndex, maxRecords, tableName);
                    return ds;
                }
                adapter.Fill(ds, tableName);
            }
            catch (SqlException exception)
            {
                throw exception;
            }
            return ds;
        }

        public DataSet runSQLQuery(string query, string datasetName, string tableName, int startIndex, int maxRecords, CommandType commandType)
        {
            return this.runSQLQuery(query, new DataSet(datasetName), tableName, startIndex, maxRecords, commandType);
        }

        public IDataReader runSQLReader(string query, CommandBehavior behavior)
        {
            return this.runSQLReader(query, CommandType.Text, behavior);
        }

        public IDataReader runSQLReader(string query, CommandType commandType)
        {
            SqlCommand command = new SqlCommand
            {
                CommandTimeout = 0,
                CommandType = commandType,
                CommandText = query,
                Connection = this.conn
            };
            return command.ExecuteReader();
        }

        public IDataReader runSQLReader(string query, CommandType commandType, CommandBehavior behavior)
        {
            SqlCommand command = new SqlCommand
            {
                CommandTimeout = 0,
                CommandType = commandType,
                CommandText = query,
                Connection = this.conn
            };
            return command.ExecuteReader(behavior);
        }

        public IDataReader runSQLReader(string query, CommandType commandType, CommandBehavior behavior, IDbTransaction trans)
        {
            SqlCommand command = new SqlCommand
            {
                CommandTimeout = 0,
                CommandType = commandType,
                Transaction = (SqlTransaction)trans,
                CommandText = query,
                Connection = this.conn
            };
            return command.ExecuteReader(behavior);
        }

        public int updateBLOB(string tableName, string fieldName, string whereString, Stream fs)
        {
            int size = 0x8000;
            SqlCommand command = new SqlCommand("SET NOCOUNT ON;UPDATE " + tableName + " SET " + fieldName + " = 0x0 " + whereString + ";SELECT @Pointer=TEXTPTR(" + fieldName + ") FROM " + tableName + " " + whereString, this.conn);
            SqlParameter parameter = command.Parameters.Add("@Pointer", SqlDbType.VarBinary, 100);
            parameter.Direction = ParameterDirection.Output;
            command.ExecuteNonQuery();
            SqlCommand command2 = new SqlCommand("UPDATETEXT " + tableName + "." + fieldName + " @Pointer @Offset @Delete WITH LOG @Bytes", this.conn)
            {
                CommandTimeout = 0
            };
            SqlParameter parameter2 = command2.Parameters.Add("@Pointer", SqlDbType.Binary, 0x10);
            SqlParameter parameter3 = command2.Parameters.Add("@Offset", SqlDbType.Int);
            SqlParameter parameter4 = command2.Parameters.Add("@Delete", SqlDbType.Int);
            parameter4.Value = 1;
            SqlParameter parameter5 = command2.Parameters.Add("@Bytes", SqlDbType.Binary, size);
            BinaryReader reader = new BinaryReader(fs);
            int num2 = 0;
            parameter3.Value = num2;
            for (byte[] buffer = reader.ReadBytes(size); buffer.Length > 0; buffer = reader.ReadBytes(size))
            {
                parameter2.Value = parameter.Value;
                parameter5.Value = buffer;
                command2.ExecuteNonQuery();
                parameter4.Value = 0;
                num2 += buffer.Length;
                parameter3.Value = num2;
            }
            reader.Close();
            return 0;
        }

        public int updateBLOBData(string query, string paramName, byte[] b, CommandType commandType)
        {
            SqlCommand command = new SqlCommand
            {
                CommandTimeout = 0,
                CommandType = commandType,
                CommandText = query,
                Connection = this.conn
            };
            SqlParameter parameter = new SqlParameter(paramName, SqlDbType.Image, b.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, b);
            command.Parameters.Add(parameter);
            return command.ExecuteNonQuery();
        }

        internal static string[] generateParamNames(string query)
        {
            int num = query.Split(new char[] { '@' }).Length - 1;
            if (num <= 0)
            {
                return null;
            }
            string[] strArray = new string[num];
            string[] strArray2 = query.Split(new char[] { '@' });
            for (int i = 1; i < strArray2.Length; i++)
            {
                strArray[i - 1] = "@";
                for (int j = 0; j < strArray2[i].Length; j++)
                {
                    string[] strArray3;
                    IntPtr ptr;
                    if (" ,)".IndexOf(strArray2[i].Substring(j, 1)) >= 0)
                    {
                        break;
                    }
                    (strArray3 = strArray)[(int)(ptr = (IntPtr)(i - 1))] = strArray3[(int)ptr] + strArray2[i].Substring(j, 1);
                }
            }
            return strArray;
        }

        public void Dispose()
        {
            if (conn.State != ConnectionState.Closed)
                conn.Close();
            conn = null;
        }
    }
}